----------------------------------------------------------------------------
---------------- Based on CastParty3 AddOn
----------------------------------------------------------------------------

function slibMinimapButton_OnLoad(self)
	self.IsDragging = false;
	self:RegisterForClicks("LeftButtonUp");
	self:RegisterForDrag("RightButton");
	self:SetFrameLevel(self:GetFrameLevel() + 1);
end

function slibMinimapButton_OnEnter(self)
	GameTooltip:SetOwner(self,"ANCHOR_LEFT");
	GameTooltip:SetText("\229\133\173\233\173\148\229\140\133\230\143\146\228\187\182\230\142\167\229\136\182\229\143\176\n\229\143\179\233\148\174\231\167\187\229\138\168\229\155\190\230\160\135");
end

function slibMinimapButton_OnLeave()
	GameTooltip:Hide();
end

function slibMinimapButton_OnMouseDown(self)
	Slib_MinimapBtn_Icon:SetTexCoord(0,1,0,1);
	
	-- Remember where the cursor was in case the user drags
	local vCursorX, vCursorY = GetCursorPosition();
	vCursorX = vCursorX / self:GetEffectiveScale();
	vCursorY = vCursorY / self:GetEffectiveScale();
	Slib_MinimapBtn.CursorStartX = vCursorX;
	Slib_MinimapBtn.CursorStartY = vCursorY;
	local vCenterX,vCenterY = Slib_MinimapBtn:GetCenter();
	local vMinimapCenterX, vMinimapCenterY = Minimap:GetCenter();
	Slib_MinimapBtn.CenterStartX = vCenterX - vMinimapCenterX;
	Slib_MinimapBtn.CenterStartY = vCenterY - vMinimapCenterY;
end

function slibMinimapButton_OnMouseUp(self)--***
	Slib_MinimapBtn_Icon:SetTexCoord(.075,.925,.075,.925);
end

function slibMinimapButton_DragStart(self)
	self:LockHighlight();
	
	Slib_MinimapBtn:ClearAllPoints();
	self.IsDragging = true;
end

function slibMinimapButton_DragEnd(self)--***
	self:UnlockHighlight();
	Slib_MinimapBtn_Icon:SetTexCoord(.075,.925,.075,.925)

	self.IsDragging = false;
end

function slibMinimapButton_OnClick(self)
	slibUIShow();
	Slib_MinimapBtn_Icon:SetTexCoord(.075,.925,.075,.925);
end

function slibMinimapButton_UpdateDragPosition(self)
	-- Hacked up from Outfitter on curse! I know they didn't invent this concept, but, they are the only
	-- ones i could find who do it!
	
	-- Remember where the cursor was in case the user drags
	local vCursorX, vCursorY = GetCursorPosition();

	vCursorX = vCursorX / self:GetEffectiveScale();
	vCursorY = vCursorY / self:GetEffectiveScale();

	local vCursorDeltaX = vCursorX - Slib_MinimapBtn.CursorStartX;
	local vCursorDeltaY = vCursorY - Slib_MinimapBtn.CursorStartY;

	local vCenterX = Slib_MinimapBtn.CenterStartX + vCursorDeltaX;
	local vCenterY = Slib_MinimapBtn.CenterStartY + vCursorDeltaY;

	-- Calculate the angle
	local vAngle = math.atan2(vCenterX, vCenterY);

	-- Set the new position
	slibMinimapButton_SetPositionAngle(vAngle);
	
	SlashLibSaved["minimapIconAngle"] = vAngle;
end

function slibMinimapButton_SetPositionAngle(pAngle)
	local vAngle = pAngle;
	local vRadius = 80;
	vCenterX = math.sin(vAngle) * vRadius;
	vCenterY = math.cos(vAngle) * vRadius;
	Slib_MinimapBtn:SetPoint("CENTER","Minimap","CENTER",vCenterX - 1,vCenterY - 1);
end